﻿Imports CrystalDataHub.BLL.DataHub.BusinessObject

Public Class ValidateInvestor

#Region "Declare"
    Public mdtNewInvestor As DataTable
    Public mdtNewFunds As DataTable
    Private mboolCheckInvestor As Boolean = False
    Public mintProjectID As Integer
    Public mblnCheckSaved As Boolean = False
    Public mlstInvestorDetail As List(Of AddNewInvestor)
#End Region

#Region "On Load"
    ''' <summary>
    ''' Dispose the form.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ValidateInvestor_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim lcolRow As UltraGridRow = Nothing
        Dim ldataRowView As DataRowView = Nothing
        Try
            If Not mblnCheckSaved Then
                lcolRow = grdNewInvestor.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("Select").Text.Equals("True", StringComparison.CurrentCultureIgnoreCase))
                If lcolRow Is Nothing Then
                    If (MessageBox.Show("Please click OK to save changes and Cancel to use the new record.", m_strMsgBoxCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK) Then
                        btnOk.PerformClick()
                    End If
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Name, mintProjectID)
        Finally
            lcolRow = Nothing
            ldataRowView = Nothing
        End Try
        Me.Dispose()
    End Sub

    ''' <summary>
    ''' Load the initilze data.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ValidateInvestor_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadFunctionality()
    End Sub

    ''' <summary>
    ''' Cell Select property. Editable only if select column field is click.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetProperties()
        Me.grdInvestors.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        Me.grdNewInvestor.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
    End Sub

    ''' <summary>
    ''' Load the 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadFunctionality()
        If Me.Text = TitleManager.GetTitle("ValidateInvestor") Then
            mboolCheckInvestor = True
            lblAdvancedSearchText.Text = "Suggested Investor(s):"
        Else
            lblAdvancedSearchText.Text = "Suggested Fund(s):"
        End If
        grdNewInvestor.DataSource = mdtNewInvestor

        If mboolCheckInvestor Then
            LoadRelativeInvestors(mdtNewInvestor.Rows(0)("Investor Name").ToString)
        Else
            LoadRelativeInvestors(mdtNewInvestor.Rows(0)("FundName").ToString)
        End If
    End Sub

    ''' <summary>
    ''' Load the data, Investor or Fund.
    ''' </summary>
    ''' <param name="pstrInvName"></param>
    ''' <remarks></remarks>
    Private Sub LoadRelativeInvestors(ByVal pstrInvName As String)
        Dim ldtSuggestedInvestor As DataTable
        Dim lintDefaultPerSearchLength As Integer
        Dim lintCellValueLength As Integer
        Try
            ldtSuggestedInvestor = New DataTable
            lintCellValueLength = pstrInvName.Length
            lintDefaultPerSearchLength = Math.Ceiling((lintCellValueLength * CommonCLL.mintInvestorDefaultSearchPercentage) / 100)
            If lintDefaultPerSearchLength < 10 Then
                If lintCellValueLength >= 10 Then
                    lintDefaultPerSearchLength = CommonCLL.mintMinSearchLength
                End If
            End If
            ldtSuggestedInvestor = cAddModifyInvestor.GetAllSuggestedInvestor(pstrInvName, mboolCheckInvestor, lintDefaultPerSearchLength)
            grdInvestors.BeginUpdate()
            grdInvestors.SuspendRowSynchronization()
            If ldtSuggestedInvestor.Rows.Count > 0 Then
                grdInvestors.DataSource = ldtSuggestedInvestor
            Else
                ldtSuggestedInvestor = mdtNewInvestor.Clone
                grdInvestors.DataSource = ldtSuggestedInvestor
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            grdInvestors.ResumeRowSynchronization()
            grdInvestors.EndUpdate()
            ldtSuggestedInvestor = Nothing
        End Try
    End Sub
#End Region

    ''' <summary>
    ''' Change the value of column on click of cell.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdInvestors_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs) Handles grdInvestors.ClickCell
        If e.Cell IsNot Nothing Then
            If (e.Cell.Column.Header.Caption = "Select") Then
                SetFalsePreviousTrue()
            End If
        End If
        GridUpdateData()
    End Sub

    ''' <summary>
    ''' Change the value of column on click of cell.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdNewInvestor_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs) Handles grdNewInvestor.ClickCell
        If e.Cell IsNot Nothing Then
            If (e.Cell.Column.Header.Caption = "Select") Then
                SetFalsePreviousTrue()
            End If
        End If
        GridUpdateData()
    End Sub

    ''' <summary>
    ''' Update the data after change in grid.Not using
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GridUpdateData()
        grdInvestors.Update()
        grdNewInvestor.Update()
        grdInvestors.UpdateData()
        grdNewInvestor.UpdateData()
    End Sub

    ''' <summary>
    ''' Set the value to false if any is true.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SetFalsePreviousTrue()
        Dim colRow As UltraGridRow = grdInvestors.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("Select").Text.Equals("True", StringComparison.CurrentCultureIgnoreCase))
        If colRow IsNot Nothing Then
            colRow.Cells("Select").Value = False
        End If
        colRow = grdNewInvestor.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("Select").Text.Equals("True", StringComparison.CurrentCultureIgnoreCase))
        If colRow IsNot Nothing Then
            colRow.Cells("Select").Value = False
        End If
    End Sub

    ''' <summary>
    ''' Select the row and return the select row to corresponding form.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim lcolRow As UltraGridRow = Nothing
        Dim ldataRowView As DataRowView = Nothing
        Dim lrow As DataRow
        Try
            lcolRow = grdNewInvestor.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("Select").Text.Equals("True", StringComparison.CurrentCultureIgnoreCase))
            If lcolRow Is Nothing Then
                lcolRow = grdInvestors.Rows.OfType(Of UltraGridRow)().ToList().Find(Function(row) row.Cells("Select").Text.Equals("True", StringComparison.CurrentCultureIgnoreCase))
                If lcolRow Is Nothing Then
                    ShowMessage(MessageManager.GetMessage("DE_NEW_SELECTROW"))
                Else
                    mdtNewInvestor.Clear()
                    ldataRowView = CType(lcolRow.ListObject, DataRowView)

                    lrow = If(ldataRowView IsNot Nothing, ldataRowView.Row, Nothing)
                    mdtNewInvestor.ImportRow(lrow)
                    mblnCheckSaved = True
                    Me.Close()
                End If
            Else
                mblnCheckSaved = True
                Me.Close()
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Name, mintProjectID)
        Finally
            lcolRow = Nothing
            ldataRowView = Nothing
            lrow = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ldrow"></param>
    ''' <remarks></remarks>
    Private Sub GetLsitOfInvestorRowView(ByVal ldrow As DataRow)
        Dim llstNewInvestor As List(Of AddNewInvestor)
        Dim lclsNewInvestor As New AddNewInvestor
        Try
            If ldrow IsNot Nothing Then
                llstNewInvestor = New List(Of AddNewInvestor)
                lclsNewInvestor.Activist = ldrow("Activist")
                lclsNewInvestor.City = ldrow("City")
                lclsNewInvestor.Country = ldrow("Country")
                lclsNewInvestor.FundID = ldrow("FundId")
                lclsNewInvestor.FundName = ldrow("FundName")
                lclsNewInvestor.HeadInvestor = ldrow("Head Investor")
                lclsNewInvestor.ID = ldrow("InvEntityID")
                lclsNewInvestor.InvestorID = ldrow("InvestorID")
                lclsNewInvestor.InvestorName = ldrow("InvestorName")
                lclsNewInvestor.InvStatusCode = ldrow("Status")
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, Me.Text, mintProjectID)
        Finally
            llstNewInvestor = Nothing
            lclsNewInvestor = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    ''' <summary>
    ''' Hide the GroupByBox
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdInvestors_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdInvestors.InitializeLayout
        grdInvestors.DisplayLayout.GroupByBox.Hidden = True
        If mboolCheckInvestor Then
            e.Layout.Bands(0).Columns("InvestorID").Hidden = True
            e.Layout.Bands(0).Columns("InvEntityID").Hidden = True
            e.Layout.Bands(0).Columns("InvestorID").CellDisplayStyle = CellDisplayStyle.PlainText
            e.Layout.Bands(0).Columns("InvEntityID").CellDisplayStyle = CellDisplayStyle.PlainText
        Else
            e.Layout.Bands(0).Columns("FundID").Hidden = True
            e.Layout.Bands(0).Columns("FndEntityID").Hidden = True
            e.Layout.Bands(0).Columns("FundID").CellDisplayStyle = CellDisplayStyle.PlainText
            e.Layout.Bands(0).Columns("FndEntityID").CellDisplayStyle = CellDisplayStyle.PlainText
        End If
        Me.grdInvestors.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        e.Layout.Bands(0).Columns("Select").CellClickAction = CellClickAction.Edit
    End Sub

    ''' <summary>
    ''' Hide the GroupByBox
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub grdNewInvestor_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdNewInvestor.InitializeLayout
        grdNewInvestor.DisplayLayout.GroupByBox.Hidden = True
        If mboolCheckInvestor Then
            e.Layout.Bands(0).Columns("InvestorID").Hidden = True
            e.Layout.Bands(0).Columns("InvEntityID").Hidden = True
            e.Layout.Bands(0).Columns("InvestorID").CellDisplayStyle = CellDisplayStyle.PlainText
            e.Layout.Bands(0).Columns("InvEntityID").CellDisplayStyle = CellDisplayStyle.PlainText
        Else
            e.Layout.Bands(0).Columns("FundID").Hidden = True
            e.Layout.Bands(0).Columns("FndEntityID").Hidden = True
            e.Layout.Bands(0).Columns("FundID").CellDisplayStyle = CellDisplayStyle.PlainText
            e.Layout.Bands(0).Columns("FndEntityID").CellDisplayStyle = CellDisplayStyle.PlainText
        End If
        Me.grdNewInvestor.DisplayLayout.Override.CellClickAction = CellClickAction.CellSelect
        e.Layout.Bands(0).Columns("Select").CellClickAction = CellClickAction.Edit
    End Sub
End Class